package com.okex.demo.repository;

import com.okex.demo.model.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
    
    // 根据产品ID查找账户
    Optional<Account> findByInstId(String instId);
    
    // 根据产品ID的基础部分（例如BTC）查找账户 
    @Query("SELECT a FROM Account a WHERE UPPER(SUBSTRING(a.instId, 1, LOCATE('-', a.instId) - 1)) = UPPER(:baseId)")
    List<Account> findByInstIdBasePartStartingWith(@Param("baseId") String baseId);
    
    // 根据产品ID模糊匹配
    @Query("SELECT a FROM Account a WHERE a.instId LIKE %:instIdPart%")
    List<Account> findByInstIdContaining(@Param("instIdPart") String instIdPart);
    
    // 查找所有有产品ID的账户
    @Query("SELECT a FROM Account a WHERE a.instId IS NOT NULL")
    List<Account> findAllWithInstId();
    
    // 通过账户名称查找
    Optional<Account> findByAccountName(String accountName);
} 